package com.ruoyi.binding.service.impl;

import java.util.Arrays;
import java.util.List;

import com.ruoyi.binding.domain.MhBinding;
import com.ruoyi.binding.service.IMhBindingService;
import com.ruoyi.common.annotation.DataScope;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.binding.mapper.MhUserGameDispatchConfigMapper;
import com.ruoyi.binding.domain.MhUserGameDispatchConfig;
import com.ruoyi.binding.service.IMhUserGameDispatchConfigService;

/**
 * 自动派单Service业务层处理
 * 
 * @author Ekko
 * @date 2025-06-26
 */
@Service
public class MhUserGameDispatchConfigServiceImpl implements IMhUserGameDispatchConfigService 
{
    @Autowired
    private MhUserGameDispatchConfigMapper mhUserGameDispatchConfigMapper;

    @Autowired
    private IMhBindingService mhBindingService;

    /**
     * 查询自动派单
     * 
     * @param id 自动派单主键
     * @return 自动派单
     */
    @Override
    @DataScope(userAlias = "u", deptAlias = "d")
    public MhUserGameDispatchConfig selectMhUserGameDispatchConfigById(Long id)
    {
        return mhUserGameDispatchConfigMapper.selectMhUserGameDispatchConfigById(id);
    }

    /**
     * @return
     */
    @Override
    public List<MhUserGameDispatchConfig> selectAutoDispatchUser() {
        MhUserGameDispatchConfig mhUserGameDispatchConfig = new MhUserGameDispatchConfig();
        mhUserGameDispatchConfig.setAutoDispatch(1);
       return mhUserGameDispatchConfigMapper.selectMhUserGameDispatchConfigList(mhUserGameDispatchConfig);
    }

    /**
     * @return
     */
    @Override
    public List<MhUserGameDispatchConfig> selectAllAutoDispatch() {
       return mhUserGameDispatchConfigMapper.selectMhUserGameDispatchConfigList(new MhUserGameDispatchConfig());
    }

    /**
     * 查询自动派单列表
     * 
     * @param mhUserGameDispatchConfig 自动派单
     * @return 自动派单
     */
    @Override
    @DataScope(userAlias = "u", deptAlias = "d")
    public List<MhUserGameDispatchConfig> selectMhUserGameDispatchConfigList(MhUserGameDispatchConfig mhUserGameDispatchConfig)
    {
        return mhUserGameDispatchConfigMapper.selectMhUserGameDispatchConfigList(mhUserGameDispatchConfig);
    }

    /**
     * 查询自动派单列表
     *
     * @param mhUserGameDispatchConfig 自动派单
     * @return 自动派单
     */
    @Override
    public List<MhUserGameDispatchConfig> selectConfigListNoDataScope(MhUserGameDispatchConfig mhUserGameDispatchConfig)
    {
        return mhUserGameDispatchConfigMapper.selectConfigListNoDataScope(mhUserGameDispatchConfig);
    }

    /**
     * 新增自动派单
     * 
     * @param mhUserGameDispatchConfig 自动派单
     * @return 结果
     */
    @Override
    public int insertMhUserGameDispatchConfig(MhUserGameDispatchConfig mhUserGameDispatchConfig)
    {
        mhUserGameDispatchConfig.setCreateBy(SecurityUtils.getUsername());
        mhUserGameDispatchConfig.setUpdateBy(SecurityUtils.getUsername());
        mhUserGameDispatchConfig.setUserId(SecurityUtils.getUserId());
        mhUserGameDispatchConfig.setCreateTime(DateUtils.getNowDate());

        try {
            return mhUserGameDispatchConfigMapper.insertMhUserGameDispatchConfig(mhUserGameDispatchConfig);
        } catch (org.springframework.dao.DuplicateKeyException e) {
            // 解析唯一约束异常信息，返回友好提示
            throw new ServiceException("当前游戏“" + mhUserGameDispatchConfig.getGameName() + "”已经配置过，请点击修改");
        }
    }

    /**
     * 修改自动派单
     * 
     * @param mhUserGameDispatchConfig 自动派单
     * @return 结果
     */
    @Override
    public int updateMhUserGameDispatchConfig(MhUserGameDispatchConfig mhUserGameDispatchConfig)
    {
        mhUserGameDispatchConfig.setUpdateTime(DateUtils.getNowDate());
        return mhUserGameDispatchConfigMapper.updateMhUserGameDispatchConfig(mhUserGameDispatchConfig);
    }

    /**
     * 批量删除自动派单
     * 
     * @param ids 需要删除的自动派单主键
     * @return 结果
     */
    @Override
    public int deleteMhUserGameDispatchConfigByIds(Long[] ids)
    {
        return mhUserGameDispatchConfigMapper.deleteMhUserGameDispatchConfigByIds(ids);
    }

    /**
     * 删除自动派单信息
     * 
     * @param id 自动派单主键
     * @return 结果
     */
    @Override
    public int deleteMhUserGameDispatchConfigById(Long id)
    {
        return mhUserGameDispatchConfigMapper.deleteMhUserGameDispatchConfigById(id);
    }

    /**
     * @return
     */
    @Override
    public List<String> getUserGameList() {
//       获取当前用户id
        Long userId = SecurityUtils.getUserId();
//        获取到的结果,分割并且放入list中
        MhBinding mhBinding = mhBindingService.selectMhBindingByUserId(userId);
        String games = mhBinding.getSuGames();
        return Arrays.asList(games.split(","));
    }

    /**
     * @param taskId
     * @return
     */
    @Override
    public MhUserGameDispatchConfig selectMhUserGameDispatchConfigByTaskId(Long taskId) {
        return mhUserGameDispatchConfigMapper.selectMhUserGameDispatchConfigByTaskId(taskId);
    }
}
